<template>
  <div name="Setting">
    <el-button
      type="success"
      icon="el-icon-edit"
      @click="showDialogVisible = true"
      >修改密码</el-button
    >
    <el-dialog title="修改密码" :visible.sync="showDialogVisible" width="50%">
      <el-input v-model="password" placeholder="请输入修改后的密码"></el-input>
      <span slot="footer" class="dialog-footer">
        <el-button @click="noChange">取 消</el-button>
        <el-button type="primary" @click="changePassword">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { changePwd } from "../../api/users";
export default {
  name: "Setting",
  props: {},
  components: {},
  data() {
    return {
      password: null,
      showDialogVisible: false,
    };
  },

  methods: {
    noChange() {
      this.password = null;
      this.showDialogVisible = false;
    },
    async changePassword() {
      if (this.password.length < 6) {
        this.$message.error({
          message: "密码长度不足6位!",
          duration: 1000,
        });
      } else {
        try {
          const res = await changePwd(this.password);
          if (res.status === 200) {
            this.$message({
              type: "success",
              message: "修改成功",
              duration: 1000,
            });
            this.password = null;
            this.showDialogVisible = false;
          }
        } catch (err) {
          this.$message({
            type: "error",
            message: "修改失败",
            duration: 1000,
          });
          this.password = null;
        }
        this.showDialogVisible = false;
      }
    },
  },
};
</script>

<style scoped></style>
